package com.ybfenpan.service;


import com.ybfenpan.model.*;
import com.ybfenpan.model.log.LogEle;
import com.ybfenpan.model.log.LogMoney;
import com.ybfenpan.model.log.LogOperAdmin;
import com.ybfenpan.service.core.BaseService;
import com.ybfenpan.service.log.*;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class MemberService extends BaseService {


    @Resource
    private DailiService dailiService;
    @Resource
    private WageCurrentService wageCurrentService;

    @Resource
    private ParamWageService paramWageService;
    @Resource
    private LogEleService logEleService;
    @Resource
    private LogMoneyService logMoneyService;
    @Resource
    private LogOperAdminService logOperAdminService;
    @Resource
    private TelValService telValService;
    @Resource
    private LogShuiService logShuiService;
    @Resource
    private PanService panService;



    @Resource
    private LogFutouService logFutouService;

    @Resource
    private LogCenterUpService logCenterUpService;

    /**
     * 查询第一个报单的人
     *
     * @return
     */
    public Member executFirst() {

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("accountActive", "T");
        param.put("pagination.orderBy", " id asc");  //排列
        List<Member> list = queryForList("select", param);

        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }


    }

    /**
     * 查询第一个成为代理的人
     *
     * @return
     */
    public Member executFirstLevel() {

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("accountActive", "T");
        param.put("where", "1=1 ");
        param.put("pagination.orderBy", " id asc");  //排列

        List<Member> list = queryForList("select", param);

        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }


    }

    /**
     * 根据登录名查询账号
     *
     * @param loginName
     * @return
     */
    public Member executMemberByLoginName(String loginName) {
        if (StringUtil.isNotBlank(loginName)) {
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("loginName", loginName);
            List<Member> members = queryForList("select", param);
            if (null != members && 0 < members.size()) {
                return members.get(0);
            }
        }
        return null;
    }

    public Member executMemberByTel(String tel) {
        if (StringUtil.isNotBlank(tel)) {
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("tel", tel);
            List<Member> members = queryForList("select", param);
            if (null != members && 0 < members.size()) {
                return members.get(0);
            }
        }
        return null;
    }


    /**
     * 注册
     *
     * @param member
     */
    public void saveOne(Member member) {
// 新增代理商
        this.saveOrUpdate(member);

        Integer memberId = member.getId();

        // 新增工资表
        WageCurrent wageCurrent = new WageCurrent();
        wageCurrent.setMemberId(memberId); // 代理商编号
        wageCurrentService.saveOrUpdate(wageCurrent);

        // 更新对象信息
        Map<String, Object> mapMember = new HashMap<String, Object>();
        Integer wageCurrentId = wageCurrent.getId();

        mapMember.put("id", memberId);
        mapMember.put("wageCurrentId", wageCurrentId);

        // 设置自己和接点人的关系
        Integer fatherId = member.getFatherId();
        Member father = this.getByKey(fatherId);
        if (father != null) {
            Map<String, Object> mapFather = new HashMap<String, Object>();
            mapFather.put("id", father.getId());
            if (member.getQu() == 1) {
                mapFather.put("zuoId", memberId);
            } else if (member.getQu() == 2) {
                mapFather.put("youId", memberId);
            }

            this.update(mapFather);
        }
        this.update(mapMember);


    }

    /**
     * @功能 奖金解冻
     */
    public void executCheckDongMoney() {

        Map<String, Object> logMap = new HashMap<String, Object>();
        logMap.put("isFa", "F");
        logMap.put("where", "1=1");
        List<LogMoney> listMoney = this.logMoneyService.queryForList("select", logMap);

        for (LogMoney lm : listMoney) {

            Double dongNum = lm.getDongNum();
            String title = lm.getTitle();
            Integer memberId = lm.getMemberId();

            Map<String, Object> memberMap = new HashMap<String, Object>();
            memberMap.put("id", memberId);
            if (lm.getState() == 1) {
                memberMap.put("diyUpdate", "money = money +" + dongNum + ",wage1=wage1+" + dongNum);
            } else if (lm.getState() == 2) {
                memberMap.put("diyUpdate", "money = money +" + dongNum + ",wage2=wage2+" + dongNum);
            } else if (lm.getState() == 3) {
                memberMap.put("diyUpdate", "money = money +" + dongNum + ",wage3=wage3+" + dongNum);
            }

            this.update(memberMap);

            Member member = this.getByKey(memberId);
            Double yuMoney = member.getMoney();
            // 新增记录

            // 金积分日志(奖金2)
            LogMoney log4 = new LogMoney();
            log4.setMemberId(memberId); // 所属代理商
            log4.setMemberLoginName(member.getLoginName());
            log4.setTitle("奖金解冻：" + title); // 日志说明
            log4.setState(lm.getState()); // 类型
            log4.setNum(dongNum); // 数量
            log4.setTotalNum(dongNum);
            log4.setDongNum(0d);
            log4.setYu(yuMoney);
            log4.setCommitDate(new Date());
            log4.setFaDate(new Date()); // 时间
            logMoneyService.saveOrUpdate(log4);

            // 更新日志
            logMap = new HashMap<String, Object>();
            logMap.put("id", lm.getId());
            logMap.put("isFa", "T");
            this.logMoneyService.update(logMap);

        }

    }

    /**
     * @功能 出局奖金解冻
     */
    public void executDongOutMoney(Integer id) {

        Map<String, Object> logMap = new HashMap<String, Object>();
        logMap.put("isFa", "F");
        logMap.put("where", "1=1 and state=4 and memberId="+id+"");
        List<LogMoney> listMoney = this.logMoneyService.queryForList("select", logMap);

        for (LogMoney lm : listMoney) {

            Double dongNum = lm.getDongNum();
            String title = lm.getTitle();
            Integer memberId = lm.getMemberId();

            Map<String, Object> memberMap = new HashMap<String, Object>();
            memberMap.put("id", memberId);
            memberMap.put("diyUpdate", "money = money +" + dongNum + ",wage2=wage2+" + dongNum);

            this.update(memberMap);

            Member member = this.getByKey(memberId);
            Double yuMoney = member.getMoney();
            // 新增记录

            // 金积分日志(奖金2)
            LogMoney log4 = new LogMoney();
            log4.setMemberId(memberId); // 所属代理商
            log4.setMemberLoginName(member.getLoginName());
            log4.setTitle("奖金解冻：" + title); // 日志说明
            log4.setState(lm.getState()); // 类型
            log4.setNum(dongNum); // 数量
            log4.setTotalNum(dongNum);
            log4.setDongNum(0d);
            log4.setYu(yuMoney);
            log4.setCommitDate(new Date());
            log4.setFaDate(new Date()); // 时间
            logMoneyService.saveOrUpdate(log4);

            // 更新日志
            logMap = new HashMap<String, Object>();
            logMap.put("id", lm.getId());
            logMap.put("isFa", "T");
            this.logMoneyService.update(logMap);

        }

    }

    /***
     * 删除
     * @param id
     */
    public void deleteOne(Integer id) {
        Member one = this.getByKey(id);
        Integer wageCurrentId = one.getWageCurrentId();

        //删除手机验证码
        TelVal telVal = telValService.executValByTel(one.getTel());
        telValService.delete(telVal);

        //删除工资表
        this.wageCurrentService.delete(wageCurrentId);


        //清除其父亲的对应的左右区
        Integer fatherId = one.getFatherId();
        if (one.getQu() != null) {

            if (fatherId != null) {
                Member father = this.getByKey(fatherId);

                Map<String, Object> mapFather = new HashMap<>();
                mapFather.put("id", father.getId());

                if (one.getQu() == 1) {

                    mapFather.put("diyUpdate", "zuoId=null");

                }
                if (one.getQu() == 2) {

                    mapFather.put("diyUpdate", "youId=null");
                }

                this.update(mapFather);
            }


        }


        //删除代理商
        this.delete(one);

    }


    /**
     * 报单
     *
     * @param id
     */
    public void saveActive(Integer id, Integer activeId, Integer payType) {
        Member one = this.getByKey(id);

        // 查询所有用到的系统参数
        Map<String, Object> mapParamWage = new HashMap<String, Object>();
        mapParamWage.put("where", "1=1 and id in (1000,1001,1002,1004,1003) ");
        List<ParamWage> paramWageList = this.paramWageService.queryForList("select", mapParamWage);

        Map<Integer, ParamWage> paramWageMap = new HashMap<Integer, ParamWage>();
        for (ParamWage p : paramWageList) {
            paramWageMap.put(p.getId(), p);
        }

        Double payNum = paramWageMap.get(1004).getValue();

        if (one.getAccountActive().equals("F")) {

            Map<String, Object> mapActiveOne = new HashMap<>();
            mapActiveOne.put("id", id);
            /******************  修改代理商信息      **********************/
            mapActiveOne.put("accountActive", "T");

            mapActiveOne.put("activeId", activeId);
            mapActiveOne.put("activeDate", new Date());
            mapActiveOne.put("payNum", payNum);

            this.update(mapActiveOne);

            //扣报单人的报单积分
            one = this.getByKey(id);

            if (activeId != null) {

                Member active = this.getByKey(activeId);
                if (active != null) {
                    //代理商报单人扣除报单码
//                    double eleActive = paramWageMap.get(1014).getValue();
                    Map mapActive = new HashMap();
                    mapActive.put("id", activeId);
                    if (payType == 10) {
                        mapActive.put("diyUpdate", "money = money-" + payNum);
                        //新增记录
                        LogMoney logMoney = new LogMoney();
                        logMoney.setMemberId(activeId);
                        logMoney.setMemberLoginName(active.getLoginName());
                        logMoney.setTitle("报单代理商：" + one.getLoginName());
                        logMoney.setState(888);
                        logMoney.setNum(-payNum.doubleValue());
                        logMoney.setYu(active.getMoney() - payNum);
                        logMoneyService.saveOrUpdate(logMoney);
                    } else if (payType == 20) {
                        mapActive.put("diyUpdate", "ele = ele-" + payNum);

                        LogEle logEle = new LogEle();
                        logEle.setMemberId(activeId);
                        logEle.setMemberLoginName(active.getLoginName());
                        logEle.setTitle("报单代理商：" + one.getLoginName());
                        logEle.setState(88);
                        logEle.setNum(-payNum.doubleValue());
                        logEle.setYu(active.getEle() - payNum);
                        logEleService.saveOrUpdate(logEle);
                    }
                    //更新余额
                    this.update(mapActive);

                }

            } else {

                Admin admin = ContextUtil.getAdmin();

                LogOperAdmin log = new LogOperAdmin();
                log.setType(10);                //操作类别(10系统登录)
                log.setAdminId(admin.getId());
                log.setTitle("管理员报单");
                log.setCommitDate(new Date());
                log.setContext("报单代理商：" + one.getLoginName());
                logOperAdminService.saveOrUpdate(log);
            }
        }

        /*****************  系统参数的报单人数**********************/

        // 今日报单、累计报单
        mapParamWage = new HashMap<>();
        mapParamWage.put("where", "id = 2000 or id = 2002  ");//
        mapParamWage.put("diyUpdate", "value = value + 1");
        paramWageService.update(mapParamWage);

        /**************  给推荐人增加直推人数,直推业绩,直推奖 **************/
        Integer recommendId = one.getRecommendId();
        Member recommend = this.getByKey(recommendId);
        if (recommend != null) {
            Map<String, Object> mapRecommend = new HashMap<>();
            mapRecommend.put("id", recommendId);
            mapRecommend.put("diyUpdate", "recommendNum=RecommendNum+1,recommendYe=recommendYe+" + payNum);

            this.update(mapRecommend);

            //当天记录
            Integer wageCurrentId = recommend.getWageCurrentId();
            Map<String, Object> mapWageCurrent = new HashMap<>();
            mapWageCurrent.put("id", wageCurrentId);
            mapWageCurrent.put("diyUpdate", "recommendNum=recommendNum+1,recommendYe=recommendYe+" + payNum);

            wageCurrentService.update(mapWageCurrent);

            //直推奖
            recommend = this.getByKey(recommendId);
            Double wage1 = paramWageMap.get(1000).getValue();
            this.saveCountWage1(recommend, one, wage1);

            if (recommend.getRecommendNum() == paramWageMap.get(1003).getValue().intValue()) {
                this.executDongOutMoney(recommend.getId());
            }

            /*******************************一条线增加团队人数和团队业绩******************************/

            Integer reId = recommend.getId();
            Member re = this.getByKey(reId);

            while (re != null) {
                //团队业绩

                Map<String, Object> paramReMap = new HashMap<>();
                paramReMap.put("id", re.getId());
                paramReMap.put("diyUpdate", "recommendTotalNum=recommendTotalNum+1,recommendTotalYe=recommendTotalYe+" + payNum);
                this.update(paramReMap);
                //当天记录
                Map<String, Object> paramWaMap = new HashMap<>();
                paramWaMap.put("id", wageCurrentId);
                paramWaMap.put("diyUpdate", "recommendTotalNum=recommendTotalNum+1,recommendTotalYe=recommendTotalYe+" + payNum);

                wageCurrentService.update(paramWaMap);

                re = this.getByKey(re.getRecommendId());
            }

        }

        /*******************************分盘开始******************************/
        if (recommend == null) {
            Pan pan = new Pan();
            pan.setPanId(one.getId());
            pan.setIsUse("F");
            this.panService.saveOrUpdate(pan);
            one = this.getByKey(one.getId());
            one.setPanId(pan.getId());
            this.update(one);
        } else {
            Double wage2 = paramWageMap.get(1001).getValue();  //出局奖
            Double wage3 = paramWageMap.get(1002).getValue();   //直推代理商出局奖

            Integer recommendPanId = recommend.getPanId();
            Pan recommendPan = this.panService.getByKey(recommendPanId);
            one.setPanId(recommendPanId);
            this.update(one);
            one=this.getByKey(one.getId());
            if (recommendPan != null) {
                if (recommendPan.getIsUse().equals("F")) {
                    if (recommendPan.getZuoPanId() == null) {
                        recommendPan.setZuoPanId(one.getId());
                    } else if (recommendPan.getYouPanId() == null) {

                        recommendPan.setYouPanId(one.getId());

                    } else if (recommendPan.getZuoPanZuoId() == null) {

                        recommendPan.setZuoPanZuoId(one.getId());

                    } else if (recommendPan.getZuoPanYouId() == null) {

                        recommendPan.setZuoPanYouId(one.getId());

                    } else if (recommendPan.getYouPanZuoId() == null) {

                        recommendPan.setYouPanZuoId(one.getId());

                    } else if (recommendPan.getYouPanYouId() == null) {
                        recommendPan.setYouPanYouId(one.getId());
                        recommendPan.setIsUse("T");
                        Member member=this.getByKey(recommendPan.getPanId());
                        this.saveCountWage2(member, one, wage2);
                        Member re=this.getByKey(member.getRecommendId());
                        if (re!=null){
                            this.saveCountWage3(re,wage3);
                        }
                        this.panService.update(recommendPan);
                        //出局分盘
                        createNewPan(member);

                    }
                    this.panService.update(recommendPan);
                }
            }

        }

    }

    //创建新盘
    private void createNewPan(Member member) {

        Pan memberPan=this.panService.getByKey(member.getPanId());
        Integer zuoId=memberPan.getZuoPanId();
        Integer youId=memberPan.getYouPanId();
        Member zuoMember=this.getByKey(zuoId);
        Member zuoPanZuoMember=this.getByKey(memberPan.getZuoPanZuoId());
        Member zuoPanYouMember=this.getByKey(memberPan.getZuoPanYouId());
        Member youMember=this.getByKey(youId);
        Member youPanZuoMember=this.getByKey(memberPan.getYouPanZuoId());
        Member youPanYouMember=this.getByKey(memberPan.getYouPanYouId());

        Member recommend=this.getByKey(member.getRecommendId());
        if (recommend==null){
            Pan pan=new Pan();
            pan.setIsUse("F");
            pan.setPanId(member.getId());
            this.panService.saveOrUpdate(pan);
            member = this.getByKey(member.getId());
            member.setPanId(pan.getId());
            this.update(member);
        }else {
            Integer recommendPanId = recommend.getPanId();
            Pan recommendPan = this.panService.getByKey(recommendPanId);
            member.setPanId(recommendPanId);
            this.update(member);
            member=this.getByKey(member.getId());
            if (recommendPan != null) {
                if (recommendPan.getIsUse().equals("F")) {
                    if (recommendPan.getZuoPanId() == null) {
                        recommendPan.setZuoPanId(member.getId());
                    } else if (recommendPan.getYouPanId() == null) {

                        recommendPan.setYouPanId(member.getId());

                    } else if (recommendPan.getZuoPanZuoId() == null) {

                        recommendPan.setZuoPanZuoId(member.getId());

                    } else if (recommendPan.getZuoPanYouId() == null) {

                        recommendPan.setZuoPanYouId(member.getId());

                    } else if (recommendPan.getYouPanZuoId() == null) {

                        recommendPan.setYouPanZuoId(member.getId());

                    } else if (recommendPan.getYouPanYouId() == null) {
                        recommendPan.setYouPanYouId(member.getId());
                        recommendPan.setIsUse("T");
                        //再分盘
                        createNewPan(recommend);

                    }
                    this.panService.update(recommendPan);
                }
            }

        }

        Pan zuoPan=new Pan();
        zuoPan.setIsUse("F");
        zuoPan.setPanId(zuoMember.getId());
        this.panService.saveOrUpdate(zuoPan);
        zuoMember.setPanId(zuoPan.getId());
        this.update(zuoMember);

        zuoPan.setZuoPanId(zuoPanZuoMember.getId());
        zuoPanZuoMember.setPanId(zuoPan.getId());
        this.update(zuoPanZuoMember);

        zuoPan.setYouPanId(zuoPanYouMember.getId());
        zuoPanYouMember.setPanId(zuoPan.getId());
        this.update(zuoPanYouMember);
        this.panService.saveOrUpdate(zuoPan);

        Pan youPan=new Pan();
        youPan.setIsUse("F");
        youPan.setPanId(youMember.getId());
        this.panService.saveOrUpdate(youPan);

        youMember.setPanId(youPan.getId());
        youPan.setZuoPanId(youPanZuoMember.getId());
        this.update(youMember);
        youPanZuoMember.setPanId(youPan.getId());
        this.update(youPanZuoMember);
        youPan.setYouPanId(youPanYouMember.getId());
        youPanYouMember.setPanId(youPan.getId());
        this.update(youPanYouMember);
        this.panService.saveOrUpdate(youPan);
    }

    /************************ 放到第二层的人的盘 ***************************/
    public void getJinPan(int panId, int memberId) {

        Pan zuoPan = this.panService.getByKey(panId);
        if (zuoPan != null) {
            if (zuoPan.getZuoPanId() == null) {
                zuoPan.setZuoPanId(memberId);
                this.panService.update(zuoPan);

            } else if (zuoPan.getYouPanId() == null) {
                zuoPan.setYouPanId(memberId);
                this.panService.update(zuoPan);
            }

        }
    }

    /**
     * 推荐奖
     */
    private void saveCountWage1(Member member, Member son, Double wage1) {

        if (wage1 > 0) {

            String title = "";
            //判断该代理商是否停薪
            if (member.getStateWage().equals("F")) {
                wage1 = 0d;
                title = title + "(工资已冻结)";
            }

            if (member.getAccountUse().equals("F")) {
                wage1 = 0d;
                title = title + "(账户已冻结)";
            }

            Double yuMoney = member.getMoney();

            if (member.getAccountBuy().equals("T")) {

                Map<String, Object> paramMember = new HashMap<>();
                paramMember.put("id", member.getId());
                paramMember.put("diyUpdate", " wage1=wage1+" + wage1 + ",historyWage=HistoryWage+" + wage1 + ",money=money+" + wage1);
                this.update(paramMember);
                member = this.getByKey(member.getId());
                yuMoney = member.getMoney();
                //日志
                LogMoney log1 = new LogMoney();
                log1.setMemberId(member.getId());
                log1.setMemberLoginName(member.getLoginName());//所属代理商
                log1.setTitle("推荐奖，报单代理商：" + son.getLoginName() + title);
                log1.setState(1);                                        //类型
                log1.setNum(wage1);                                            //数量
                log1.setTotalNum(wage1);
                log1.setDongNum(0d);
                log1.setYu(yuMoney);
                log1.setIsFa("T");
                log1.setCommitDate(new Date());
                log1.setFaDate(new Date());        //时间
                logMoneyService.saveOrUpdate(log1);

            } else {

                Map<String, Object> paramMember = new HashMap<>();
                paramMember.put("id", member.getId());
                paramMember.put("diyUpdate", " store=store+" + wage1);
                this.update(paramMember);
                ParamWage paramWage1005 = this.paramWageService.getByKey(1005);
                if (member.getStore() >= paramWage1005.getValue()) {

                    paramMember = new HashMap<>();
                    paramMember.put("id", member.getId());
                    paramMember.put("diyUpdate", " wage1=wage1+" + wage1 + ",historyWage=HistoryWage+" + wage1 + ",money=money+" + wage1);
                    this.update(paramMember);
                    member = this.getByKey(member.getId());
                    yuMoney = member.getMoney();
                    //日志
                    LogMoney log1 = new LogMoney();
                    log1.setMemberId(member.getId());
                    log1.setMemberLoginName(member.getLoginName());//所属代理商
                    log1.setTitle("推荐奖，报单代理商：" + son.getLoginName() + title);
                    log1.setState(1);                                        //类型
                    log1.setNum(wage1);                                            //数量
                    log1.setTotalNum(wage1);
                    log1.setDongNum(0d);
                    log1.setYu(yuMoney);
                    log1.setIsFa("T");
                    log1.setCommitDate(new Date());
                    log1.setFaDate(new Date());        //时间
                    logMoneyService.saveOrUpdate(log1);
                } else {

                    LogMoney log1 = new LogMoney();
                    log1.setMemberId(member.getId());
                    log1.setMemberLoginName(member.getLoginName());//所属代理商
                    log1.setTitle("推荐奖，报单代理商：" + son.getLoginName() + title);
                    log1.setState(1);                                        //类型
                    log1.setNum(wage1);                                            //数量
                    log1.setTotalNum(wage1);
                    log1.setIsFa("F");
                    log1.setDongNum(wage1);
                    log1.setYu(yuMoney);
                    log1.setCommitDate(new Date());
                    logMoneyService.saveOrUpdate(log1);
                }

            }

            //改动当前工资
            Integer wageCurrentId = member.getWageCurrentId();
            Map<String, Object> paramWageCurrent = new HashMap<>();
            paramWageCurrent.put("id", wageCurrentId);
            paramWageCurrent.put("diyUpdate", " wage1=wage1+" + wage1 + ",wageNum=WageNum+" + wage1);
            this.wageCurrentService.update(paramWageCurrent);

        }

    }


    /**
     * 出局
     */
    private void saveCountWage2(Member member, Member son, Double wage2) {

        if (wage2 > 0) {

            String title = "";
            //判断该代理商是否停薪
            if (member.getStateWage().equals("F")) {
                wage2 = 0d;
                title = title + "(工资已冻结)";
            }

            if (member.getAccountUse().equals("F")) {
                wage2 = 0d;
                title = title + "(账户已冻结)";
            }

            Double yuMoney = member.getMoney();

            if (member.getAccountBuy().equals("T")) {
                ParamWage paramWage1003 = this.paramWageService.getByKey(1003);
                if (member.getRecommendNum() >= paramWage1003.getValue()) {

                    Map<String, Object> paramMember = new HashMap<>();
                    paramMember.put("id", member.getId());
                    paramMember.put("diyUpdate", " wage2=wage2+" + wage2 + ",historyWage=HistoryWage+" + wage2 + ",money=money+" + wage2);
                    this.update(paramMember);

                    member = this.getByKey(member.getId());
                    yuMoney = member.getMoney();
                    //日志
                    LogMoney log1 = new LogMoney();
                    log1.setMemberId(member.getId());
                    log1.setMemberLoginName(member.getLoginName());//所属代理商
                    log1.setTitle("出局奖：" + son.getLoginName() + title);
                    log1.setState(2);                                        //类型
                    log1.setNum(wage2);                                            //数量
                    log1.setTotalNum(wage2);
                    log1.setIsFa("T");
                    log1.setDongNum(0d);
                    log1.setYu(yuMoney);
                    log1.setCommitDate(new Date());
                    log1.setFaDate(new Date());        //时间
                    logMoneyService.saveOrUpdate(log1);
                } else {

                    LogMoney log1 = new LogMoney();
                    log1.setMemberId(member.getId());
                    log1.setMemberLoginName(member.getLoginName());//所属代理商
                    log1.setTitle("出局奖(推荐人数不够)：" + son.getLoginName() + title);
                    log1.setState(4);                                        //类型
                    log1.setNum(wage2);                                            //数量
                    log1.setTotalNum(wage2);
                    log1.setIsFa("F");
                    log1.setDongNum(wage2);
                    log1.setYu(yuMoney);
                    log1.setCommitDate(new Date());
                    logMoneyService.saveOrUpdate(log1);

                }

            } else {

                Map<String, Object> paramMember = new HashMap<>();
                paramMember.put("id", member.getId());
                paramMember.put("diyUpdate", " store=store+" + wage2);
                this.update(paramMember);
                ParamWage paramWage1005 = this.paramWageService.getByKey(1005);
                if (member.getStore() >= paramWage1005.getValue()) {

                    paramMember = new HashMap<>();
                    paramMember.put("id", member.getId());
                    paramMember.put("diyUpdate", " wage2=wage2+" + wage2 + ",historyWage=HistoryWage+" + wage2 + ",money=money+" + wage2);
                    this.update(paramMember);

                    member = this.getByKey(member.getId());
                    yuMoney = member.getMoney();
                    //日志
                    LogMoney log1 = new LogMoney();
                    log1.setMemberId(member.getId());
                    log1.setMemberLoginName(member.getLoginName());//所属代理商
                    log1.setTitle("出局奖：" + son.getLoginName() + title);
                    log1.setState(2);                                        //类型
                    log1.setNum(wage2);                                            //数量
                    log1.setTotalNum(wage2);
                    log1.setIsFa("T");
                    log1.setDongNum(0d);
                    log1.setYu(yuMoney);
                    log1.setCommitDate(new Date());
                    log1.setFaDate(new Date());        //时间
                    logMoneyService.saveOrUpdate(log1);
                } else {

                    //日志
                    LogMoney log1 = new LogMoney();
                    log1.setMemberId(member.getId());
                    log1.setMemberLoginName(member.getLoginName());//所属代理商
                    log1.setTitle("出局奖：" + son.getLoginName() + title);
                    log1.setState(2);                                        //类型
                    log1.setNum(wage2);                                            //数量
                    log1.setTotalNum(wage2);
                    log1.setIsFa("F");
                    log1.setDongNum(wage2);
                    log1.setYu(yuMoney);
                    log1.setCommitDate(new Date());
                    logMoneyService.saveOrUpdate(log1);
                }
            }

            //改动当前工资
            Integer wageCurrentId = member.getWageCurrentId();
            Map<String, Object> paramWageCurrent = new HashMap<>();
            paramWageCurrent.put("id", wageCurrentId);
            paramWageCurrent.put("diyUpdate", " wage2=wage2+" + wage2 + ",wageNum=WageNum+" + wage2);
            this.wageCurrentService.update(paramWageCurrent);

        }

    }


    /**
     * 直推代理商出局奖
     */
    public void saveCountWage3(Member member, Double wage3) {

        if (wage3 > 0) {

            String title = "";
            //判断该代理商是否停薪
            if (member.getStateWage().equals("F")) {
                wage3 = 0d;
                title = title + "(工资已冻结)";
            }

            if (member.getAccountUse().equals("F")) {
                wage3 = 0d;
                title = title + "(账户已冻结)";
            }

            Double yuMoney = member.getMoney();
            if (member.getAccountBuy().equals("T")) {

                Map<String, Object> paramMember = new HashMap<>();
                paramMember.put("id", member.getId());

                paramMember.put("diyUpdate", " wage3=wage3+" + wage3 + ",historyWage=historyWage+" + wage3 + ",money = money +" + wage3);
                this.update(paramMember);
                //日志

                member = this.getByKey(member.getId());
                yuMoney = member.getMoney();

                LogMoney log1 = new LogMoney();
                log1.setMemberId(member.getId());
                log1.setMemberLoginName(member.getLoginName());//所属代理商
                log1.setTitle("直推代理商出局奖：" + title);
                log1.setState(3);                                        //类型
                log1.setNum(wage3);                                            //数量
                log1.setTotalNum(wage3);
                log1.setIsFa("T");
                log1.setDongNum(0d);
                log1.setYu(yuMoney);
                log1.setCommitDate(new Date());
                log1.setFaDate(new Date());        //时间
                logMoneyService.saveOrUpdate(log1);

            } else {

                Map<String, Object> paramMember = new HashMap<>();
                paramMember.put("id", member.getId());
                paramMember.put("diyUpdate", " store=store+" + wage3);
                this.update(paramMember);
                ParamWage paramWage1005 = this.paramWageService.getByKey(1005);
                if (member.getStore() >= paramWage1005.getValue()) {

                    paramMember = new HashMap<>();
                    paramMember.put("id", member.getId());
                    paramMember.put("diyUpdate", " wage3=wage3+" + wage3 + ",historyWage=HistoryWage+" + wage3 + ",money=money+" + wage3);
                    this.update(paramMember);

                    member = this.getByKey(member.getId());
                    yuMoney = member.getMoney();
                    //日志
                    LogMoney log1 = new LogMoney();
                    log1.setMemberId(member.getId());
                    log1.setMemberLoginName(member.getLoginName());//所属代理商
                    log1.setTitle("直推代理商出局奖：" + title);
                    log1.setState(3);                                        //类型
                    log1.setNum(wage3);                                            //数量
                    log1.setTotalNum(wage3);
                    log1.setIsFa("T");
                    log1.setDongNum(0d);
                    log1.setYu(yuMoney);
                    log1.setCommitDate(new Date());
                    log1.setFaDate(new Date());        //时间
                    logMoneyService.saveOrUpdate(log1);

                } else {

                    LogMoney log1 = new LogMoney();
                    log1.setMemberId(member.getId());                                        //所属代理商
                    log1.setTitle("直推代理商出局奖" + title);
                    log1.setState(3);                                        //类型
                    log1.setNum(wage3);                                            //数量
                    log1.setTotalNum(wage3);
                    log1.setIsFa("F");
                    log1.setDongNum(wage3);
                    log1.setYu(yuMoney);
                    log1.setCommitDate(new Date());
                    logMoneyService.saveOrUpdate(log1);
                }

            }

            //改动当前工资
            Integer wageCurrentId = member.getWageCurrentId();
            Map<String, Object> paramWageCurrent = new HashMap<>();
            paramWageCurrent.put("id", wageCurrentId);
            paramWageCurrent.put("diyUpdate", " wage3=wage3+" + wage3 + ",wageNum=WageNum+" + wage3);
            this.wageCurrentService.update(paramWageCurrent);

        }

    }

}
